﻿using DotNetty.Buffers;
using DotNetty.Transport.Channels;
using System.Text;

namespace HandlerOrderServer
{
    public class HandleServerLast : SimpleChannelInboundHandler<IByteBuffer> // 泛型使用IByteBuffer
    {
        private int count = 0;

        protected override void ChannelRead0(IChannelHandlerContext ctx, IByteBuffer msg)
        {
            Console.WriteLine("HandleServerLast1");
            // 如果第一次接收到数据,就给客户端返回server to client
            if (count == 0)
            {
                count++;
                // 因为这里用的是IByteBuffer类型,所以写入客户端需要将字符串转换成IByteBuffer
                ctx.WriteAndFlushAsync(Unpooled.CopiedBuffer(Encoding.UTF8.GetBytes("server to client")));
            }
            Console.WriteLine("HandleServerLast2");
        }
    }
}